package com.ruoyi.web.controller.tongban;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tongban.domain.FacePerson;
import com.ruoyi.tongban.service.IFacePersonService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备注册用户Controller
 * 
 * @author ruoyi
 * @date 2021-04-08
 */
@RestController
@RequestMapping("/tongban/person")
public class FacePersonController extends BaseController
{
    @Autowired
    private IFacePersonService facePersonService;

    /**
     * 查询设备注册用户列表
     */
    @PreAuthorize("@ss.hasPermi('tongban:person:list')")
    @GetMapping("/list")
    public TableDataInfo list(FacePerson facePerson)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(!SecurityUtils.isAdmin(user.getUserId())){
            facePerson.setDeptId(user.getDeptId());
        }
        startPage();
        List<FacePerson> list = facePersonService.selectFacePersonList(facePerson);
        return getDataTable(list);
    }

    @GetMapping("addPerson")
    public AjaxResult addPerson(@RequestParam Long personId){
        return facePersonService.addPeople(personId);
    }
    
    @GetMapping("addPersonPc")
    public AjaxResult addPersonPc(@RequestParam Long personId){
        return facePersonService.addPeople(personId);
    }

    /**
     * 导出设备注册用户列表
     */
    @PreAuthorize("@ss.hasPermi('tongban:person:export')")
    @Log(title = "设备注册用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FacePerson facePerson)
    {
        List<FacePerson> list = facePersonService.selectFacePersonList(facePerson);
        ExcelUtil<FacePerson> util = new ExcelUtil<FacePerson>(FacePerson.class);
        return util.exportExcel(list, "person");
    }

    /**
     * 获取设备注册用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('tongban:person:query')")
    @GetMapping(value = "/{personId}")
    public AjaxResult getInfo(@PathVariable("personId") Long personId)
    {
        return AjaxResult.success(facePersonService.selectFacePersonById(personId));
    }

    /**
     * 新增设备注册用户
     */
    @PreAuthorize("@ss.hasPermi('tongban:person:add')")
    @Log(title = "设备注册用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FacePerson facePerson)
    {
        return toAjax(facePersonService.insertFacePerson(facePerson));
    }

    /**
     * 修改设备注册用户
     */
    @PreAuthorize("@ss.hasPermi('tongban:person:edit')")
    @Log(title = "设备注册用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FacePerson facePerson)
    {
        int index = facePersonService.updateFacePerson(facePerson);
        if(index != 0){
            return AjaxResult.success();
        }
        
        return AjaxResult.error("此卡已绑定学生,请勿重复绑定");
    }

    /**
     * 删除设备注册用户
     */
    @PreAuthorize("@ss.hasPermi('tongban:person:remove')")
    @Log(title = "设备注册用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{personIds}")
    public AjaxResult remove(@PathVariable Long[] personIds)
    {
        return toAjax(facePersonService.deleteFacePersonByIds(personIds));
    }
}
